package org.yanghaokun.user.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.yanghaokun.basic.exception.BusinessException;
import org.yanghaokun.basic.util.SmsUtil;
import org.yanghaokun.basic.util.StrUtils;
import org.yanghaokun.user.constant.UserConstant;
import org.yanghaokun.user.domain.User;
import org.yanghaokun.user.mapper.UserMapper;
import org.yanghaokun.user.service.IUserService;
import org.yanghaokun.user.service.IVerifyCodeService;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class VerifyCodeServiceImpl implements IVerifyCodeService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    @Qualifier("redisTemplate")
    private RedisTemplate redisTemplate;

    @Override
    public void sendSms(Map<String,String> params) {
        String phone = params.get("phone");
        String type = params.get("type");
        //先校验手机号是否位null
        if(!StringUtils.hasLength(phone)) {
            //没有输入手机号
            throw new BusinessException("请输入手机号");
        }
        //下面判断验证码的类型
        if ("register".equals(type)) {//注册
            //存在用户不能注册
            User user = userMapper.findByphone(phone);
            if (user != null) {
                throw new BusinessException("该用户已经存在");
            }
            //是注册我们就把标识设置为注册
            String businessKey = UserConstant.REDIS_PREFIX + phone;
            sendSmscode(businessKey);
        }else if ("binder".equals(type)) {
            String businessKey = UserConstant.BINDER_PREFIX + phone;
            sendSmscode(businessKey);
        }else if ("login".equals(type)) {
            String businessKey = UserConstant.LOGIN_PREFIX + phone;
            sendSmscode(businessKey);
        }
    }

    private void sendSmscode(String businessKey) {
        //发送验证码，先判断之前的验证码是否有效,因为验证码一般都是存在redis中所以我们先去看看是否为空,这里我们定义一个常量提高效率防止写错
        Object codeObj = redisTemplate
                .opsForValue().get(businessKey);
        String code = "";
        //我们先看代码是否有效
        if (codeObj != null) {
            //将获取到的对象转换成String类型，分割获取第二个也就是xxxx：time时间
            String codeStr = (String) codeObj;
            String time = codeStr.split(":")[1];
            //当前时间减去剩余的重发时间还小于重发时间就不行
            //没有超过就给出错误提示
            if (System.currentTimeMillis() - Long.valueOf(time) <= 1 * 60 * 1000) {
                throw new BusinessException("请勿重复发送验证码");
            }
            //超过，但是验证码没有失效我们就继续保存
            code = codeStr.split(":")[0];
        }else {//代码失效了我们就重新生成验证码
            code = StrUtils.getComplexRandomString(4);
            //将验证码和当前时间保存到redis
            Long currentMills = System.currentTimeMillis();
            redisTemplate.opsForValue().set(businessKey, code + ":" + currentMills , 3, TimeUnit.MINUTES);
        }
        //最后调用短信接口发送短信
//        SmsUtil.sendMsg(phone,"您的验证码为" + code + "请在三分钟内携码洗jio！！！");
        System.out.println("您的验证码为" + code + "请在三分钟内携码洗jio！！！");
    }

}
